package com.baichang.taoshunda.dao.impl;

import com.baichang.framework.model.Page;
import org.springframework.stereotype.Repository;

import com.baichang.framework.dao.impl.BaseDaoImpl;
import com.baichang.taoshunda.dao.UserRechargeHistoryDao;
import com.baichang.taoshunda.entity.UserRechargeHistory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class UserRechargeHistoryDaoImpl extends BaseDaoImpl<UserRechargeHistory> implements UserRechargeHistoryDao {

    @Override
    public void updateStatus(int status, String itemName, int userRechargeId) {
        String sql = "UPDATE `t_user_recharge_history` SET `payment_unit`=?,status=? WHERE (`id`= ?)";
        update(sql, itemName, status, userRechargeId);
    }

    /**
     * @param name
     * @param telephone
     * @param nowPage
     * @param pageSize  @return
     */
    @Override
    public Page queryUserLife(String name, String telephone, int nowPage, int pageSize) {
        List list = new ArrayList();
        String sql = "SELECT * FROM t_user_recharge_history WHERE status=1";
        if (!name.equals("")) {
            sql += " AND user_name LIKE ?";
            list.add("%" + name + "%");
        }
        if (!telephone.equals("")) {
            sql += " AND account LIKE ?";
            list.add("%" + telephone + "%");
        }
        return pageForMapList(sql, nowPage, pageSize, list.toArray());
    }

    /**
     * 查询充值记录
     *
     * @param userId
     * @param type
     * @param nowPage
     * @param pageSize
     * @return
     */
    @Override
    public List<Map<String, Object>> getUserRechargeHistory(int userId, String type, int nowPage, int pageSize) {
        String sql = "SELECT urh.id, urh.account,urh.item_id, urh.recharge_type, urh.amount, urh.created , urh.payment_unit, urh.order_number, ua.type AS accountType FROM t_user_recharge_history urh LEFT JOIN t_user_account ua ON ua.user_account = urh.account WHERE urh.user_id = ? AND urh.recharge_type = ? AND urh.`status`=1";
        sql += " order by id desc";
        return listForMapList(sql, nowPage, pageSize, userId, type);
    }

    @Override
    public UserRechargeHistory getHistoryByOrderNumber(String outer_tid) {
        String sql = "SELECT * FROM t_user_recharge_history WHERE order_number=? ";
        return queryForBean(sql, outer_tid);
    }

}